package com.tes;

import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;

public class SelectLevel extends BaseGameActivity implements
		IOnMenuItemClickListener {

	// SCREEN SIZE
	static final int CAMERA_WIDTH = 480;
	static final int CAMERA_HEIGHT = 320;

	// MENU
	static final int LEVEL_1 = 1;
	static final int LEVEL_2 = 2;
	static final int LEVEL_3 = 3;
	static final int LEVEL_4 = 4;
	static final int LEVEL_5 = 5;
	static final int LEVEL_6 = 6;
	static final int LEVEL_7 = 7;
	static final int LEVEL_8 = 8;
	static final int LEVEL_9 = 9;
	static final int LEVEL_10 = 10;

	// MENU SCENE
	private MenuScene menuScene;

	// CAMERA
	private Camera mCamera;

	// FONT
	private BitmapTextureAtlas tFontMenu;
	private Font mFontMenu;

	// Set biar ada musicnya :)
	@Override
	public Engine onLoadEngine() {
		this.mCamera = GameResource.camera;
		Engine engine = new Engine(new EngineOptions(true,
				ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(
						CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)
				.setNeedsMusic(true).setNeedsSound(true));
		return engine;
	}

	@Override
	public void onLoadResources() {
		// path
		FontFactory.setAssetBasePath("font/");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		SoundFactory.setAssetBasePath("sound/");

		// menu
		tFontMenu = GameResource.tFontChiller;
		mFontMenu = GameResource.mFontChiller;

		// load resource
		mEngine.getTextureManager().loadTextures(tFontMenu);
		mEngine.getFontManager().loadFont(mFontMenu);
	}

	// Return pada menuScene-nya :)
	@Override
	public Scene onLoadScene() {
		this.menuScene = createMenuStart();

		return this.menuScene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {

		Intent intent = new Intent(this, TesGame3.class);
		GameResource.SelectedLevel = pMenuItem.getID();
		this.finish();
		startActivity(intent);

		return false;
	}

	protected MenuScene createMenuStart() {
		MenuScene mScene = new MenuScene(this.mCamera);

		float x = 50;
		float y = 30;

		for (int i = 0; i < GameResource.levelTiledMap.size(); i++) {
			int level = i + 1;
			if (level == 6) {
				x += (CAMERA_WIDTH / 2);
				y = 30;
			}

			IMenuItem menuItem = new ColorMenuItemDecorator(new TextMenuItem(
					level, this.mFontMenu, "Level " + level), 1.0f, 0.0f, 0.0f,
					0.0f, 0.0f, 1.0f);
			menuItem.setPosition(x, y);
			y += menuItem.getHeight() + 10;
			mScene.addMenuItem(menuItem);
		}

		mScene.setBackgroundEnabled(true);
		// R-G-B Kasih warna background
		mScene.setBackground(new ColorBackground(1.0f, 1.0f, 1.0f));
		mScene.setOnMenuItemClickListener(this);

		return mScene;
	}
}
